package cn.bopomo.dealer.base.action.ajax;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.RequestUtils;

import cn.bopomo.dealer.base.action.BaseAction;
import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.util.ResultCode;
import cn.bopomo.dealer.base.vo.AuthorizeList;
import cn.bopomo.dealer.base.vo.ModuleList;

public class AddAuthorizeToModuleAjax extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//AJAX返回设置
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		
		StringBuffer result = new StringBuffer();
		
		AuthorizeList authorize;
		String op_result;
		try {
			//获取对象
			authorize = new AuthorizeList();
			RequestUtils.populate(authorize, request); // 从相应的表单中获取数据(前提:属性名必须一致)
			//AuthorizeList authorize2 = new AuthorizeList();
			//BeanUtils.copyProperties(authorize2,form);
			//获取参数
			Integer mId = new Integer(request.getParameter("moduleRef"));
			//String mId = request.getParameter("moduleRef");
			ModuleList module = new ModuleList();
			module.setMid(mId);
			authorize.setModuleList(module);
			authorize.setAid(null);
			//获取Service
			IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
			//检查对象--"权限"
			op_result = os.checkAuthorizeValidate(authorize);
			if (ResultCode.OP_SUCCESS.equals(op_result)){
				//新增对象--"权限"
				op_result = os.addAuthorize(authorize);
			}
			log.info(authorize.getAid() + ":" + authorize.getAuthorizeName());
		} catch (Exception e) {
			log.error(e);
			op_result = ResultCode.OP_FAILED;
		}
		//组合返回信息
		result.append(op_result).append("||")
									.append(this.getCodeDescription(op_result,request.getSession().getServletContext().getResource(this.codeDescXMLPath).getPath()));
		log.info(result);
		//返回 
		response.getWriter().write(result.toString());
		return null;
		
	}
	

}
